<?php
session_start();
include 'nav.inc.php';
include 'dbConn.php';
html_head('Nutzer bearbeiten');

echo("<link rel='stylesheet' type='text/css' href='styleVerteiler.css' media='all' />\n");
echo("<link rel='stylesheet' type='text/css' href='tcal.css' />");
echo("<script type='text/javascript' src='tcal.js'></script>\n");
echo "<script type='text/javascript' src='verteiler.js'></script>";

menu();

$pid = -1;
$doubleEntry = 0;
echo("<div class='content'>\n");

if (!isset($_REQUEST['submitBTN']) && isset($_REQUEST['pid'])) {
 
	$pid = $_REQUEST['pid'];
	
	$result = $dbVerteiler->getMemberByPID($pid);
	$user = mysql_fetch_array($result);
	$vorname = $user['Vorname'];
	$nachname = $user['Nachname'];
	$comment = $user['Kommentar'];
	$until = $user['Bis'];
} 

if (isset($_REQUEST['submitBTN']) || $_REQUEST['force'] > 0) 
{
	$pid = $_REQUEST['pid'];
	$vorname = $_REQUEST['vorname'];
	$nachname = $_REQUEST['nachname'];
	$comment = $_REQUEST['content'];
	$until = $_REQUEST['day'];

	$resultCheck = $dbVerteiler->getMemberByName($vorname, $nachname);
	$falseAlarm = false;
	if ($_REQUEST['force'] < 0 && mysql_num_rows($resultCheck) > 0) 
	{
		$onlymyself = true;
		while ($row = mysql_fetch_array($resultCheck)) 
		{
			$onlymyself = $onlymyself && $row["PID"] == $pid;
		}
		if (!$onlymyself)
		{
			$result = $dbVerteiler->getMemberByPID($pid);
			$row = mysql_fetch_array($result);
			echo "<script language=javascript>
					var sName=prompt('Achtung: \\n\\nEine Person mit dem Namen \"".$vorname." ".$nachname."\" exisitert bereits in der Datenbank. \\nBitte geben Sie einen eindeutigen Namen an, oder bestätigen sie den Doppeleintrag durch einen Klick auf \'OK\'.', '".$vorname." ".$nachname."');
					var vorname = '".$row["Vorname"]."';
					var nachname = '".$row['Nachname']."';
					if (sName != null)	
					{
						var names = sName.split(' ');
						vorname = names[0];		
						for( var k=1; k<names.length-1; k++ ) {
							vorname = vorname + ' ' + names[k];
						}
						nachname = names[k];
					} 
					setModUser(vorname, nachname);
				</script>";
			$doubleEntry = 1;
		} else
			$falseAlarm = true;
	} 
	if (!($_REQUEST['force'] < 0) || !(mysql_num_rows($resultCheck) > 0)  || $falseAlarm)
	{
		$dbVerteiler->UpdatePerson($pid, $_REQUEST['vorname'], $_REQUEST['nachname'], $_REQUEST['day'], $_REQUEST['content']);
	}
}
echo "<form id='modUser' name='modUser' action='".$_SERVER['SCRIPT_NAME']."' method='post' >\n";
$result = $dbVerteiler->getMembers();
echo "\t<fieldset>
  <legend>Wähle Nutzer</legend>
";
	echo "<div id='userselect'><select id='members' name='members' size=30 onchange='showUserInfo();'>";
while ($row = mysql_fetch_array($result))
{
	echo "<option value='".$row['PID']."'>".$row['Vorname']." ".$row['Nachname']."</option>";
}
echo "</select></div>
  </fieldset>";
echo "\t<fieldset>
  <legend id='userInfoLegend' style='display:none;'> Bearbeite Nutzer</legend>
<ol id='userInfo' style='display:none;' class='clearfix'>
    <li>
      <label for='vorname'>Vorname:</label>
      <input type='text' name='vorname' id='vorname' value='".$vorname."' />
    </li>
    <li>
      <label for='nachname'>Nachname:</label>
      <input type='text' name='nachname' id='nachname' value='".$nachname."' />
      <input type='hidden' name='pid' id='pid' value='".$pid."' />
      <input type='hidden' name='force' id='force' value='-1' />
      <input type='hidden' name='index' id='index' value='-1' />
    </li>

    <li>
      <label for='day'>Bis? (falls bekannt):</label>
		<input type='text' name='day' id='day' class='tcal' value='".$until."' />
    </li>
    <li>
      <label for='content'>Kommentar:</label>
		<textarea cols='45' rows='4' name='content' id='content'>".$comment."</textarea>
    </li>
    	<li class='last'>
      		<input type='submit' name='submitBTN' id='submitBTN' value='Ändern' />";
    	echo "</li>
		<br/><br/> Hinweis: Adressen und Listenzugehörigkeit der Nutzer muss über die entsprechenden Untermenu geändert werden
  	</ol>
	
  </fieldset>";
	echo "\t</form>\n";

if ($doubleEntry)
	{
			echo "<script language=javascript>
					changeModUser(".$pid.");
				</script>";
	}
	
if ($pid > -1)
		echo "<script type='text/javascript'>showNowForReal('".$_REQUEST["index"]."');</script>";
echo "</div>";

?>
